package com.xidian.yunwudemo.service.impl;

import com.xidian.yunwudemo.dto.DataViewUserDto;
import com.xidian.yunwudemo.mapper.master.DataUserAuthMapper;
import com.xidian.yunwudemo.service.DataViewUserService;
import com.xidian.yunwudemo.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DataViewUserServiceImpl implements DataViewUserService {
    @Autowired
    DataUserAuthMapper dataUserAuthMapper;
    @Override
    public DataViewUserDto getUserDayData() {
        Date dNow = new Date(); // 当前时间
        String[] timeScope = TimeUtil.TIME_UTIL.OneDay(dNow);
        String[][] temp = dataFormat(dataUserAuthMapper.getScopeData(timeScope[0],timeScope[1]));
        return new DataViewUserDto(temp[0],temp[1]);
    }

    @Override
    public DataViewUserDto getUserWeekData() {
        Date dNow = new Date();
        String[] timeScope = TimeUtil.TIME_UTIL.OneWeek(dNow);
        String[][] temp = dataFormat(dataUserAuthMapper.getScopeData(timeScope[0],timeScope[1]));
        return new DataViewUserDto(temp[0],temp[1]);
    }

    @Override
    public DataViewUserDto getUserAllData() {
        String[][] temp = dataFormat(dataUserAuthMapper.getAllTimeData());
        return new DataViewUserDto(temp[0],temp[1]);
    }

    private String[][] dataFormat(List<Map> res) {
        Map<String,String > map = new HashMap<>();
        int len = res.size();
        String[][] data = new String[2][len];
        for(int i = 0; i < len; i++){
            data[0][i] = String.valueOf(res.get(i).get("authCount"));
            data[1][i] = String.valueOf(res.get(i).get("avgLastingTime"));
//            System.out.println(i+":"+data[0][i]+","+data[1][i]);
        }
        return data;
    }
}
